/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentacion.dialogos;

import conexion.Conexion;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import presentacion.comun.FramePortDB;
import presentacion.comun.tabla;

/**
 *
 * @author Henry
 */
public class Paso1 extends javax.swing.JDialog {

    Statement stmt;
    Conexion conn;
    Connection c;
    FramePortDB parent;
    List<String> lista = new ArrayList();
    DefaultTableModel modelo = new DefaultTableModel();
    JCheckBox check = new JCheckBox();
    private List<tabla> tblInfo = new ArrayList();
    
    private SiguienteAction siguienteAction = new SiguienteAction();

    public Paso1(FramePortDB parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parent= parent;
    }

    public Paso1(FramePortDB parent, boolean modal, Conexion conexion, List<String> lista) {
        super(parent, modal);

        for (String string : lista) {

            this.tblInfo.add(new tabla(string, true));
        }
         this.parent= parent;

        initComponents();

        this.conn = conexion;
        this.lista = lista;
        try {
            c = conn.getConexionOracle();
            // renderTabla();
        } catch (SQLException ex) {
            Logger.getLogger(Paso1.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jCheckBox1 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Selección de objetos"));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${tblInfo}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding.setColumnName("Tabla");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${seleccionado}"));
        columnBinding.setColumnName("Agregar");
        columnBinding.setColumnClass(Boolean.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setMinWidth(60);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(60);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(60);

        jCheckBox1.setText("Incluir Vistas");

        jButton1.setAction(siguienteAction);
        jButton1.setText("Siguiente");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                .addComponent(jButton1))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCheckBox1))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(5, 5, 5))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(5, 5, 5))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    }//GEN-LAST:event_formWindowActivated

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws SQLException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Paso1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Paso1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Paso1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Paso1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Paso1 dialog = new Paso1(new FramePortDB(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);

            }
        });


    }

    public final void renderTabla() {
        modelo.addColumn("Tablas", lista.toArray());
        modelo.addColumn("Seleccionar");
        jTable1.setModel(modelo);
        // TableColumn columna = jTable1.getColumnModel().getColumn(1);
        // columna.setCellEditor(new DefaultCellEditor(check));
        jTable1.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean isFocused,
                    int row, int column) {

                boolean marked = (Boolean) value;
                JCheckBox rendererComponent = new JCheckBox();
                if (marked) {
                    rendererComponent.setSelected(true);
                }
                return rendererComponent;
            }
        });
    }
//     public class NextAction extends AbstractAction {
//
//        public NextAction() {
//            putValue(NAME, "Probar");
//            
//        }
//
//        @Override
//        public void actionPerformed(ActionEvent ae) {
//            SwingWorker worker = new SwingWorker() {
//                @Override
//                protected Object doInBackground() throws Exception {
//                    jProgressBar1.setIndeterminate(true);
//                    try {
//                        if (conexion.getConexionOracle() != null) {
//                            jProgressBar1.setIndeterminate(false);
//                            jProgressBar1.setString("Conexion Correcta.");
//                            btnAceptar.setEnabled(true);
//                            return true;
//                        }
//                    } catch (SQLException ex) {
//                        jProgressBar1.setIndeterminate(false);
//                        jProgressBar1.setString("Error al Conectar al Servidor.");
//                        return false;
//                    }
//                    jProgressBar1.setIndeterminate(false);
//                    return false;
//                }
//            };
//            
//             worker.execute();
//
//        }
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the tblInfo
     */
    public List<tabla> getTblInfo() {
        return tblInfo;
    }

    /**
     * @param tblInfo the tblInfo to set
     */
    public void setTblInfo(List<tabla> tblInfo) {
        this.tblInfo = tblInfo;
    }

    private class SiguienteAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent ae) {
            List<List<String>> lista = new ArrayList<List<String>>();
            lista = LlenarListas();
            
            Paso2 pasito2 = new Paso2(parent, true, lista); 
            pasito2.setVisible(true);
        }
    }

    public List<List<String>> LlenarListas() {
        ResultSet rs = null;
        try {
            conn.getSquema(conn.getUser().toString(), true, true);
        } catch (SQLException ex) {
            System.out.println(""+ex.getMessage());
        }
        List<String> subLista = new ArrayList<String>();
        List<List<String>> lista = new ArrayList<List<String>>();
        try {
            rs = c.createStatement().executeQuery("select * from temp_table");
            
            while (rs.next()) {
                subLista.add(rs.getString(1));
            }
            lista.add(subLista);
            
            rs = c.createStatement().executeQuery("select * from temp_con");
            while (rs.next()) {
                subLista.add(rs.getString(1));
            }
            lista.add(subLista);
            
            rs = c.createStatement().executeQuery("select * from temp_view");
            while (rs.next()) {
                subLista.add(rs.getString(1));
            }
            lista.add(subLista);
        } catch (SQLException e) {
            System.out.println("Este "+e.getMessage());
        }
        return lista;

    }
}
